﻿using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace SharpCmd.IO
{
    internal class FileCopier
    {
        public FileCopier(FileCopierMode mode)
        {
        }

        public static bool RenameFile(string oldName, string newName)
        {
            if (!IsValidPath(newName))
            {
                MessageBox.Show(string.Format("Target filename {0} is invalid!", newName), "SharpCmd");
                return false;
            }
            if (File.Exists(newName) && (MessageBox.Show("Target file already exists. Overwrite it?", "SharpCmd", MessageBoxButtons.YesNo) != DialogResult.Yes))
                return false;
            try
            {
                File.Move(oldName, newName);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error renaming file: " + ex.Message);
                return false;
            }
        }

        public static bool IsValidPath(string path)
        {
            Regex r = new Regex(@"^(([a-zA-Z]\:)|(\\))(\\{1}|((\\{1})[^\\]([^/:*?<>""|]*))+)$");
            return r.IsMatch(path);
        }

        public static bool RenameDirectory(string oldName, string newName)
        {
            if (!IsValidPath(newName))
            {
                MessageBox.Show(string.Format("Target name {0} is invalid!", newName), "SharpCmd");
                return false;
            }
            if (Directory.Exists(newName))
            {
                MessageBox.Show(string.Format("Target directory {0} already exists!", newName), "SharpCmd");
                return false;
            }
            try
            {
                Directory.Move(oldName, newName);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error moving directory: " + ex.Message);
                return false;
            }
        }
    }
}